package com.qiantu.phone.ui.adapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.qiantu.api.utils.ThemeUtil;
import com.qiantu.phone.BuildConfig;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;
import com.qiantu.phone.event.RoomIconSelectEvent;
import com.qiantu.phone.glide.GlideApp;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class RoomIconAdapter  extends AppAdapter<Map> {
    private boolean isSelect = true;
    private boolean isSingleSelect = false;
    private Map<String, Map> mSelectedMap = new LinkedHashMap<>();

    public RoomIconAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public RoomIconAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new RoomIconAdapter.ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {
        private LinearLayout layout;
        private TextView roomName;
        private ImageView roomIcon;

        private ViewHolder() {
            super(R.layout.room_icon_item);
            layout = findViewById(R.id.room_icon_back);
            roomName = findViewById(R.id.tv_name);
            roomIcon = findViewById(R.id.icon_room);
        }

        @Override
        public void onBindView(int position) {
            Map icon = getItem(position);
            String type = icon.get("type").toString();
            roomName.setText(icon.get("name").toString());
            layout.setTag(icon);
            layout.setOnClickListener(mCheckListener);
            String imageUrl = BuildConfig.APP_IMAGE_BASE + "app/images/roomType/" + ThemeUtil.getIconThemeSuffix() + icon.get("iconName");
            GlideApp.with(roomIcon).load(imageUrl).into(roomIcon);
            if(mSelectedMap.containsKey(type)){
                layout.setSelected(true);
            }else{
                layout.setSelected(false);
            }
        }
    }

    private View.OnClickListener mCheckListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Map icon = (Map) v.getTag();
            String type = icon.get("type").toString();
            if (isSingleSelect && isSelect){//单选直接跳转
                EventBus.getDefault().post(new RoomIconSelectEvent(type,icon.get("name").toString()));
            }else if(isSingleSelect){//单选 选中状态
                if (!mSelectedMap.containsKey(type)){
                    mSelectedMap.clear();
                    mSelectedMap.put(type, icon);
                    onSelectChanged(mSelectedMap);
                }
                EventBus.getDefault().post(new RoomIconSelectEvent(type,icon.get("name").toString()));
            }else {//多选
                if (!mSelectedMap.containsKey(type)){
                    mSelectedMap.put(type, icon);
                    onSelectChanged(mSelectedMap);
                }else {
                    mSelectedMap.remove(type);
                    onSelectChanged(mSelectedMap);
                }
                EventBus.getDefault().post(new RoomIconSelectEvent(getSelectedData()));
            }
        }
    };

    public ArrayList<Map> getSelectedData() {
        ArrayList<Map> checkList = new ArrayList<>();
        for (String key : mSelectedMap.keySet()) {
            checkList.add(mSelectedMap.get(key));
        }
        return checkList;
    }

    public void setSelectedMap(List<Map> mSelectedMap) {
        if (mSelectedMap != null) {
            for (Map data : mSelectedMap) {
                String type = data.get("type").toString();
                this.mSelectedMap.put(type, data);
            }
        }
    }

    public void setSelected(Map<String, Map> selected) {
        this.mSelectedMap = selected;
    }

    public void clearSelected() {
        mSelectedMap.clear();
    }

    public void setIsSelect(boolean value) {
        isSelect = value;
    }

    public void onSelectChanged(Map<String, Map> selected) {
    }

    public boolean isIsSelect() {
        return isSelect;
    }

    public boolean isSingleSelect() {
        return isSingleSelect;
    }

    public void setSingleSelect(boolean singleSelect) {
        isSingleSelect = singleSelect;
    }

}
